package org.NooLab.docserver.properties;

import java.util.ArrayList;
import java.util.Random;


// import org.NooLab.texx.components.DataFilter;

import org.NooLab.utilities.strings.ArrUtilities;
import org.NooLab.utilities.strings.StringsUtil;
import org.NooLab.utilities.xml.XMessageAbs;
import org.NooLab.utilities.xml.XMessageIntf;


import com.jamesmurty.utils.XMLBuilder;

/**
 * 
 * this class reads and writes XML strings or files
 * and interprets them as ModelingSettings-object
 * 
 * This is very important for autonomic systems, and for a separation
 * of GUI and SOM engine
 * 
 */
public class SettingsTransporter  extends XMessageAbs implements XMessageIntf{

	DocumentObserverProperties docoProperties ;
	
	transient StringsUtil strgutils = new StringsUtil();
	
	
	// ========================================================================
	public SettingsTransporter( DocumentObserverProperties sfp ){
		docoProperties = sfp;

		
	}
	// ========================================================================
	
	/**
	 * dependent on request, exports all settings into an XML or into a serialized object
	 * @param format 0=sub sections like ModellingSettings are in different files; 1=all properties in one xml 
	 */
	public String exportProperties( int format ) {
		
		String xmlstr = "" ;
		
		XMLBuilder builder, sfgBuilder ;

		// persistenceSettings  outputSettings pluginSettings modelingSettings
		try {
 
			
			builder = getXmlBuilder( "docoproperties" ); 
			
			
			// outxBuilder = outputSettings.exportPropertiesAsXBuilder(this) ;
			
			sfgBuilder = generalSomFluidPropertiesAsXBuilder();
			
			
			if (sfgBuilder!=null){
				builder = builder.importXMLBuilder(sfgBuilder) ;
			}
			
			
			builder.up();
			
			xmlstr = getXmlStr(builder, true);
			 
			
		}catch(Exception e){
		}
		
		
		
		return xmlstr;
	}
	
	
	/**
	 * 
	 * @param docoProperties
	 * @param xmlString
	 */
	public void importSomFluidPropertiesFromXml( DocumentObserverProperties docoProperties, String xmlString ){
		int vi;
		double v;
		boolean vb;
		String str;
		
		
		setContentRoot("somfluidproperties") 	;
		
					       str = getSpecifiedInfo(xmlString,"//docoproperties/general/glueType", "value");
				       	   vi = getInt(str,0) ; // getList(str, Double.class);
		docoProperties.setGlueType( vi );

		

		/*
				sfxBuilder = sfxBuilder.e("glueType").a("value", ""+ docoProperties.getGlueType() ).up() ;
				sfxBuilder = sfxBuilder.e("sourceType").a("value", ""+ docoProperties.getSourceType() ).up() ;
				
				sfxBuilder = sfxBuilder.e("dataSrcFilename").a("value", ""+ docoProperties.getDataSrcFilename() ).up() ;
				sfxBuilder = sfxBuilder.e("dataUptakeControl").a("value", ""+ docoProperties.getDataUptakeControl() ).up() ;
				sfxBuilder = sfxBuilder.e("somType").a("value", ""+ docoProperties.getSomType() ).up() ;
				
				sfxBuilder = sfxBuilder.e("initialNodeCount").a("value", ""+ docoProperties.getInitialNodeCount() ).up() ;
				sfxBuilder = sfxBuilder.e("messagingActive").a("value", ""+ docoProperties.getMessagingActive() ).up() ;
				sfxBuilder = sfxBuilder.e("multithreadedProcesses").a("value", ""+ docoProperties.getMultithreadedProcessing() ).up() ;
				sfxBuilder = sfxBuilder.e("restrictionForSelectionSize").a("value", ""+ docoProperties.getRestrictionForSelectionSize() ).up() ;
				sfxBuilder = sfxBuilder.e("extendingDataSourceEnabled").a("value", ""+ docoProperties.isExtendingDataSourceEnabled() ).up() ;

				 				n = 0;
				 				if (docoProperties.getAbsoluteFieldExclusions()!=null){
				 					n= docoProperties.getAbsoluteFieldExclusions().size();
				 				}
				sfxBuilder = sfxBuilder.e("absoluteFieldExclusions")
												.a("count",""+n)
												.a("value", ""+serializeMonoList( docoProperties.getAbsoluteFieldExclusions()) ).up() ;  
				 
				sfxBuilder = sfxBuilder.e("absoluteFieldExclusionsMode").a("value", ""+ docoProperties.getAbsoluteFieldExclusionsMode() ).up() ;
				sfxBuilder = sfxBuilder.e("showSomProgressMode").a("value", ""+ docoProperties.getShowSomProgressMode() ).up() ; 
				sfxBuilder = sfxBuilder.e("isPluginsAllowed").a("value", ""+ strgutils.booleanize( docoProperties.isPluginsAllowed() )).up() ;
				sfxBuilder = sfxBuilder.e("algorithmsConfigPath").a("value", ""+ docoProperties.getAlgorithmsConfigPath() ).up() ;
				sfxBuilder = sfxBuilder.e("multiProcessingLevel").a("value", ""+ docoProperties.getMultiProcessingLevel() ).up() ;
				sfxBuilder = sfxBuilder.e("systemRootDir").a("value", ""+ docoProperties.getSystemRootDir() ).up() ;
		 
		 */
	}
	

	
	
	/**
	 * we could do it by means that are built in to Java... 
	 * yet, we need particular & explicit control about the info written into the XML, 
	 * and this storage should be independent from the structure of the object
	 * 
	 * @return
	 */
	private XMLBuilder generalSomFluidPropertiesAsXBuilder(){
		
		XMLBuilder sfxBuilder;
		int n;
		
		// TODO: don't forget the file reference to the settings.xml of sub-domains !!!!
		
		sfxBuilder = getXmlBuilder( "general" ); 
					
				sfxBuilder = sfxBuilder.e("glueType").a("value", ""+ docoProperties.getGlueType() ).up() ;
				
				sfxBuilder = sfxBuilder.e("somType").a("value", ""+ docoProperties.getWorkerType() ).up() ;
				
				sfxBuilder = sfxBuilder.e("messagingActive").a("value", ""+ docoProperties.getMessagingActive() ).up() ;
				sfxBuilder = sfxBuilder.e("multithreadedProcesses").a("value", ""+ docoProperties.getMultithreadedProcessing() ).up() ;
				

				 				n = 0;

				 
				sfxBuilder = sfxBuilder.e("multiProcessingLevel").a("value", ""+ docoProperties.getMultiProcessingLevel() ).up() ;
				sfxBuilder = sfxBuilder.e("systemRootDir").a("value", ""+ docoProperties.getSystemRootDir() ).up() ;
	 
		sfxBuilder.up() ;
		
		return sfxBuilder;
	}
	
	
	
	public String template( String action){ 
		 
		String xmlstr = "" ;
		XMLBuilder builder ;

		
		try {
 
			
			builder = getXmlBuilder( "messageboard" ).a( "name", "spela").a("role", "" ); 
			
			builder.e("subscription")
			
					.up() ;
			
			  
			
			xmlstr = getXmlStr(builder, true);
			 
			
		}catch(Exception e){
		}
		
		return xmlstr;
	
	}
	
	/**
	 *   
	 * @param xmlSettings  // might be an URL or the XML itself
	 * 
	 */
	public DocumentObserverProperties importProperties(String xmlSettings ) { 
		
		return null;
	}
	
}
